module main

import os

const dir_arr = ['.zed','src','include','build']
const red_tip_start = '\033[31m'
const red_tip_end = '\033[0m'

const main_cpp_name = 'main.cpp'
const main_cpp = '#include<iostream>\nusing namespace std;\nint main(){\n    cout<<"hello world"<<endl;\n    return 0;\n}'

const cmake_name = 'CMakeLists.txt'
const cmake = r'cmake_minimum_required(VERSION 3.16)
project(App LANGUAGES CXX)  # 项目名是App，因此CMAKE_PROJECT_NAME=App

# ==================== 强制使用Clang编译器 ====================
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(FATAL_ERROR "请确保已安装Clang！当前编译器: ${CMAKE_CXX_COMPILER_ID}")
endif()

# ==================== 编译配置 ====================
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ==================== Clang特定编译选项 ====================
add_compile_options(
    -Wall                # 基本警告
    -Wextra              # 额外警告
    -Wpedantic           # 严格遵循标准
    -Werror              # 将警告视为错误（可选）
    -Wconversion         # 隐式类型转换警告
    -Wno-unused-parameter # 忽略未使用参数警告（根据需要开启）
)

# 优化选项
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native" CACHE STRING "" FORCE)

# ==================== 源文件与目标 ====================
aux_source_directory(src SRC_FILES)

# 生成可执行文件，目标名是${CMAKE_PROJECT_NAME}（即App）
add_executable(${CMAKE_PROJECT_NAME} ${SRC_FILES})

# ==================== 调试信息配置（关键修正） ====================
# 目标名使用${CMAKE_PROJECT_NAME}（与add_executable中的目标一致）
# 由于项目是CXX（C++），只需配置C++的编译选项即可
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
    $<$<COMPILE_LANGUAGE:CXX>:-g>  # 仅对C++添加-g调试信息
)'
const cmake_presets_name ='CMakePresets.json'
const cmake_presets =r'{
  "version": 3,
  "configurePresets": [
    {
      "name": "clang-debug",
      "displayName": "Clang Debug",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug",
        "CMAKE_CXX_COMPILER": "clang++",
        "CMAKE_C_COMPILER": "clang"
      }
    },
    {
      "name": "clang-release",
      "displayName": "Clang Release",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/release",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release",
        "CMAKE_CXX_COMPILER": "clang++",
        "CMAKE_C_COMPILER": "clang"
      }
    }
  ]
}'

const debug_json_name ='debug.json'
const debug_json =r'[
  {
    "label": "debug app",
    "adapter": "CodeLLDB",
    "request": "launch",
    "program": "${ZED_WORKTREE_ROOT}\\build\\debug\\app.exe",
    "cwd": "$ZED_WORKTREE_ROOT",
    "build": {
      "command": "cmake --preset clang-debug && cmake --build build/debug"
    }
  }
]'




fn main() {
    // 检查命令行参数数量
    if os.args.len < 2 {
        eprintln('${red_tip_start}程序需要一个参数（项目名称）${red_tip_end}')
        return
    }
    
    mut project_name := os.args[1]
    println('your project name is ${project_name}')
    
    create_dir(dir_arr, project_name)
	
	file_info_map := {
		os.join_path(project_name,'src',main_cpp_name): main_cpp,
		os.join_path(project_name,cmake_name): cmake,
		os.join_path(project_name,cmake_presets_name): cmake_presets,
		os.join_path(project_name,'.zed',debug_json_name): debug_json,
	}
	for file_name, file_content in file_info_map {
		create_file(file_name, file_content)
	}
}

fn create_dir(dir_arr []string, project_name string) {
    // 首先创建项目主目录
    os.mkdir(project_name) or {
        eprintln('${red_tip_start}无法创建项目主目录 ${project_name}: ${err}${red_tip_end}')
        return  // 如果主目录创建失败，直接返回
    }
    
    for dir in dir_arr {
        final_dir := os.join_path(project_name, dir)
        os.mkdir(final_dir) or {
            eprintln('${red_tip_start}${dir} 目录未创建: ${err}${red_tip_end}')
            continue
        }
    }
}

fn create_file(file_name string, text string) {
    os.write_file(file_name,text) or {
        eprintln('${red_tip_start}无法创建文件 ${file_name}: ${err}${red_tip_end}')
        return
    }
}

